package com.example.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.Goods;
import com.example.domain.Order;
import com.example.service.IOrderService;
import com.example.util.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@Api(tags = "采购单管理接口")
@RestController
@RequestMapping("/order")
@PreAuthorize("hasAnyAuthority('admin')")
public class OrderController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private IOrderService orderService;

    //采购单生成应该会涉及到事务
    @PostMapping("/add")
    @ApiModelProperty("添加采购单")
    @PreAuthorize("hasAnyAuthority('buyer')")
//    public Result addOrder(@RequestBody Order order, Goods goods) {
    public Result addOrder(@RequestBody ArrayList<Object> requestData) {
        Order order = objectMapper.convertValue(requestData.get(0), Order.class);
        Goods goods = objectMapper.convertValue(requestData.get(1), Goods.class);
        return orderService.addOrder(order, goods);
//        return null;
    }

    @PostMapping("/update")
    @ApiModelProperty("修改采购单信息")
    @PreAuthorize("hasAnyAuthority('admin','supplier','buyer')")
    public Result updateOrder(@RequestBody Order order) {
        return orderService.updateOrder(order);
    }

    @GetMapping("/find")
    @ApiModelProperty("根据id查询采购单信息")
    public Result findById(Integer id) {
        System.out.println(id);
        return orderService.findById(id);
    }

    @PostMapping("/delete")
    @ApiModelProperty("删除采购单信息")

    public Result deleteOrder(@RequestBody Order order) {
        return orderService.updateOrder(order);
    }

    @GetMapping("/selectAll")
    @ApiModelProperty("查询所有采购单信息")
    public Result selectAll() {
        return orderService.selectAll();
    }

    @GetMapping("selectByBusiness")
    @ApiModelProperty("根据采购商名字查询采购单信息")
    public Result selectByBusinessOrder(String business) {
        return orderService.selectByBusinessOrder(business);
    }

}
